# -*- coding: utf-8 -*-
# @Time    : 2020/4/30 13:39
# @Author  : duyang
# @Email   : dy2903@126.com
# @File    : githubProxy.py
import re

import fire
import pyperclip

prefix = "https://gh.lework.workers.dev"


def release_code(gitUrl, tag=""):
	'''
	python githubProxy.py  release-code  hyperledger/fabric v2.1.0
	'''
	if not "/" in gitUrl:
		raise Exception("gitUrl 参数里面应该携带 '/' ")

	url = "{}/https://github.com/{}/archive/{}.tar.gz".format(prefix, gitUrl, tag)
	print(url)
	pyperclip.copy(url)
	return url


def release_file(gitUrl, tag, file):
	'''
	python githubProxy.py  release-file  hyperledger/fabric v2.1.0
	'''
	if not "/" in gitUrl:
		raise Exception("gitUrl 参数里面应该携带 '/' ")

	url = "{}/https://github.com/{}/releases/download/{}/{}".format(prefix, gitUrl, tag, file)
	print(url)
	pyperclip.copy(url)
	return url


def source_code(gitUrl, branch):
	'''
	python githubProxy.py  source-code   hyperledger/fabric master
	'''
	if not "/" in gitUrl:
		raise Exception("gitUrl 参数里面应该携带 '/' ")

	url = "{}/https://github.com/{}/archive/{}.zip".format(prefix, gitUrl, branch)

	print(url)
	pyperclip.copy(url)
	return url


def source_file(gitUrl, file, branch="master"):
	'''
	python githubProxy.py  source-file   hyperledger/fabric go.sum
	'''
	if not "/" in gitUrl:
		raise Exception("gitUrl 参数里面应该携带 '/' ")

	url = "{}/https://github.com/{}/blob/{}/{}".format(prefix, gitUrl, branch, file)
	print(url)
	pyperclip.copy(url)
	return url


def source_raw(gitUrl):
	'''
	python githubProxy.py  source-raw   https://raw.githubusercontent.com/docker/compose/1.25.x/contrib/completion/bash/docker-compose

	'''
	if not "/" in gitUrl:
		raise Exception("gitUrl 参数里面应该携带 '/' ")

	url = re.sub(r"https://raw.githubusercontent.com/(.*?)/(.*?)/", r"{}/https://github.com/\1/\2/blob/".format(prefix),
	             gitUrl)
	pyperclip.copy(url)
	return url


if __name__ == '__main__':
	fire.Fire()
